package com.cross.core;

import java.util.ArrayList;
import java.util.List;
import com.cross.util.StringUtils;

/**
 * Excel表格表头
 * 
 * @date 2009-2-4
 * @author craziness_stone
 */
public final class Header implements Cloneable {
	// 列对象列表
	private List<Column> titles = new ArrayList<Column>();
	// 子列对象列表
	private List<Header> subList = new ArrayList<Header>(0);
	
	private String objectName;

	public String getObjectName() {
		return objectName;
	}

	public void setObjectName(String objectName) {
		this.objectName = objectName;
	}

	/**
	 * 构造方法
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 */
	public Header() {

	}
	
	/**
	 * 构造方法
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @param titles
	 *            列对象列表
	 */
	public Header(final List<Column> titles) {
		this.titles.addAll(titles);
	}

	/**
	 * 根据已有表头对象创建一个新表头对象
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @param header
	 */
	public Header(final Header header) {
		refresh(header);
	}
	
	public void refresh(final Header header){
		removeAll();
		this.objectName = header.getObjectName();
		for (Column c : header.getColumns()) {
			titles.add(c.clone());
		}
	}

	/**
	 * 根据列名找出该列对象
	 * 
	 * 2009-2-4
	 * 
	 * @author craziness_stone
	 * @param name
	 * @return
	 */
	public Column getColumn(String title) {
		if (StringUtils.isEmpty(title)) {
			return null;
		}
		for (Column c : titles) {
			if (title.equalsIgnoreCase(c.getTitle())) {
				return c;
			}
		}
		return null;
	}

	/**
	 * 根据列号找出该列对象
	 * 
	 * 2009-2-4
	 * 
	 * @author craziness_stone
	 * @param colum
	 * @return
	 */
	public Column getColumn(int number) {
		return titles.get(number);
	}

	/**
	 * 表格所有列对象的列表
	 * 
	 * 2009-2-9
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public List<Column> getColumns() {
		return titles;
	}

	public int size(){
		return titles.size();
	}
	/**
	 * 添加一个列对象
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @param column
	 */
	public void add(Column column) {
		titles.add(column);
	}

	/**
	 * 移出所有表头是的列对象
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 */
	public void removeAll() {
		titles.removeAll(titles);
	}

	/**
	 * 移出指定列对象
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 * @param number
	 */
	public void remove(int number) {
		titles.remove(number);
	}

	/**
	 * 克隆
	 * 
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public Header clone() {
		Header h = new Header();
		h.setObjectName(objectName);
		for (Column c : this.titles) {
			h.add(c.clone());
		}
		return h;
	}

	/**
	 * 获得子表头列表
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone
	 * @return
	 */
	public List<Header> getSubList() {
		return subList;
	}

	/**
	 * 设置子表头对象列表
	 * 
	 * 2009-5-11
	 * 
	 * @author craziness_stone cc
	 * @param subList
	 */
	public void setSubList(List<Header> subList) {

		this.subList = subList;
	}
}
